package gatech.cs3251.project4;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.util.Log;

public class Data extends ContentProvider{
	public static final Uri CONTENT_URI = Uri.parse("content://" +
			"gatech.cs3251.project4.data");
	public static final Uri USERID = Uri.parse("content://" +
			"gatech.cs3251.project4.data/userid");
	public static final Uri LATITUDE= Uri.parse("content://" +
			"gatech.cs3251.project4.data/latitude");
	public static final Uri LONGITUDE= Uri.parse("content://" +
			"gatech.cs3251.project4.data/longitude");
	public static final Uri ALTITUDE = Uri.parse("content://" +
			"gatech.cs3251.project4.data/altitude");
	
    private static final String DATABASE_NAME    = "Locations.db";
    private static final int    DATABASE_VERSION = 3;
    private static final String TABLE_NAME       = "Locations";
    private static final String TAG              = "MyContentProvider";
    
    public static final String userIdCol = "USERID";
    public static final String latitudeCol = "LATITUDE";
    public static final String longitudeCol = "LONGITUDE";
    public static final String altitudeCol = "ALTITUDE";
    
    private static class DatabaseHelper extends SQLiteOpenHelper
    {
		public DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			Log.v("CHRIS", "DatabaseHelper onCreate");
			db.execSQL("Create table " + TABLE_NAME + "(_id INTEGER PRIMARY KEY, " +
					userIdCol + " INTEGER, " + latitudeCol + " TEXT, " +
					longitudeCol + " TEXT, " + altitudeCol + " TEXT)");
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.v("CHRIS", "DatabaseHelper onUpgrade");
			db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
			onCreate(db);
		}
    }
	
	private SQLiteDatabase sqlDB;
	private DatabaseHelper dbHelper;

	
	@Override
	public boolean onCreate() {
		Log.v("CHRIS", "Data onCreate");
		dbHelper = new DatabaseHelper(getContext());
		return (dbHelper == null) ? false : true;
	}
	
	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		Log.v("CHRIS", "Data query");
		SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		qb.setTables(TABLE_NAME);
		Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, sortOrder);
		return c;
	}
	
	@Override
	public Uri insert(Uri uri, ContentValues values) {
		Log.v("CHRIS", "Data insert");
		sqlDB = dbHelper.getWritableDatabase();
		if(values != null)
		{
			long rowId = sqlDB.insert(TABLE_NAME, "", values);
			if(rowId > 0)
			{
				return Uri.withAppendedPath(uri, String.valueOf(rowId));
			}
		}
		return null;
	}
	
	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		Log.v("CHRIS", "Data update");
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		return db.update(TABLE_NAME, values, selection, selectionArgs);
	}
	
	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		Log.v("CHRIS", "Data delete");
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		return db.delete(TABLE_NAME, selection, selectionArgs);
	}

	@Override
	public String getType(Uri uri) {
		// TODO Auto-generated method stub
		return "Derrrrp?";
	}
}
